Repository navigation
Give a conditional expression the value of its condition, not its refinement - #561
CatarinaGamboa wants to merge 3 commits into
Conversation
…inement The refinement of `c ? x : y` used the refinement of `c` as the condition of its if-then-else. That refinement constrains the condition through the wildcard, which in the conditional's refinement is the result, so a condition like `b` became `result == b` and Z3 rejected the sorts (Int, Bool, arrays or objects). The condition is now kept in a fresh boolean, on the values it was evaluated on, as the switch selector is. Compound assignments with a conditional get their condition's value the same way. The `args` of `main` were never added to the context, so any verification condition that reached them failed with a Not Found error. `main` is now loaded as any other method. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex review, round 1Codex ran about 40 small programs against this branch and a build of Should be rejected, and are rejected: ternaries between arrays, ints and booleans; nested ternaries in a branch and in the condition; ternaries returned, passed as arguments, assigned and compound-assigned ( Findings
Nothing in this round needs a change in this PR. |
…ten fields A branch that writes a field the condition reads (f ? (f = false) : true) made the condition name the written value. The condition's value is now taken before the branches are checked, and a guard snapshots the fields the method writes, as it already did for parameters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex review, round 2This round covered what round 1 did not: fields, loops and loop conditions, switch, lambdas, Findings
|
Codex review, round 3This round targeted 4b2de48, which snapshots written fields in
None of these involve conditional expressions, so they are left for separate issues. Nothing relevant remains for this PR. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clean-code reviewclean-code-principles audit of
|
Fixes #558
Root causes
Crash (
Sorts (Array Int Int) and Bool are incompatible).visitCtConditionalbuilt the refinement ofc ? x : yasite(refinement(c), refinement(x), refinement(y)). The refinement ofcconstrains the condition through the wildcard (_ == bfor a read ofb), but in the conditional's refinement the wildcard is the result. Soint[] a = b ? new int[1] : new int[1]gavea == b ? ... : ..., comparing an array with a boolean. This is not specific to arrays: on main every ternary whose condition is a variable, a literal, a call or another ternary crashes (Sorts Int and Bool,Sorts Box and Bool, ...). Only comparison conditions like(a == 2)worked, because their refinement has no wildcard.Variable 'args' could not be found. This is a separate cause, and the ternary is not needed to trigger it.main(String[])was skipped both when its contract was collected and when its body was entered, soargswas never in the context. Any verification condition that reachedargsfailed, for exampleboolean b = args.length > 0; if (b) { @Refinement("_ > 0") int n = args.length; }. The array ternary only exposed it because its condition pulledb, and soargs, into the VC.Fix
##ternary_condition_N. A refinement with the wildcard (or of a call) constrains it, and a refinement without one is its value. Like the switch selector, it names the instances the condition read, so later writes do not change which branch was taken. Conditions with side effects or no information leave it unconstrained, asassumeConditiondoes. The result then getsx's facts when the condition holds andy's when it does not.x += c ? 1 : 2goes throughOperationsChecker, which had the same problem whenc's refinement was not_ == e(e.g. a nested ternary). It now uses a fresh value there too.mainis loaded like any other method.snapshotMutableParameters→snapshotMutableVariables) now also covers fields the method writes, sof ? (f = false) : truedoes not read the writtenf. This helper also guardsifandswitch, soif (f > 0) { f = 0; x = -1; } else { x = 1; } pos(x);(accepted on main) is now rejected.Tests
CorrectTernaryExpressiongets an array ternary under its condition (args-based), and a compound assignment with a nested-ternary condition.ErrorTernaryExpressiongetsb ? new int[1] : new int[2]againstlength(_) == 1, and a field condition written in a branch. Each added line fails on main.Cases tried (one class each; all now pass or are rejected as expected):
if (b)/if (!b); as an argument, a return value and an assignment (a = b ? new int[2] : a); nested ternaries in a branch and in the condition; literal and call conditions (with and without a return refinement); condition variable or parameter written after the ternary; variable lengths; an array parameter as a branch.b ? y > 0 : truemust not assumey > 0); compound assignments.Box z = b ? x : y; z.peek()) and ghost refinements on parameters.Test suite: 364 tests, 0 failures. Study task check: 55/55 variants with identical verdicts and messages.
Known pre-existing issues, not fixed here (see the review comments): calls on a ternary receiver (
(b ? x : y).m()) are never state-checked; an int field read in an operand and written later (int y = f; f = 5;) changes whatymeans; a conditional used as anifcondition carries no information; and an assignment used as a value has no refinement.🤖 Generated with Claude Code